﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetComponents.Design.TypeConverters;
using System.Reflection;

namespace ProgNetComponentsDx.Data.Design
{
    public class SelectDataTypeConverter : CollectionItemTypeConverter<SelectData>
    {
        protected override System.Reflection.ConstructorInfo GetConstructor(SelectData item)
        {
            return typeof(SelectData).GetConstructor(new Type[] { typeof(string), typeof(RecordSelectStruct[]) });
        }

        protected override object[] GetConstructorParamters(SelectData item)
        {
            return new object[] { item.Name, item.RecordSelect.ToArray() };
        }
    }

    public class SelectDataGroupTypeConverter : ProgNetComponents.Design.TypeConverters.CollectionItemTypeConverter<SelectDataGroup>
    {
        protected override System.Reflection.ConstructorInfo GetConstructor(SelectDataGroup item)
        {
            ConstructorInfo ci = null;
            if (item.Items.Count == 0)
            {
                if (item.Groups.Count == 0)
                    ci = item.GetType().GetConstructor(new Type[] { typeof(string) });
                else
                    ci = item.GetType().GetConstructor(new Type[] { typeof(string), typeof(SelectDataGroup[]) });
            }
            else
            {
                if (item.Groups.Count == 0)
                    ci = item.GetType().GetConstructor(new Type[] { typeof(string), typeof(SelectData[]) });
                else
                    ci = item.GetType().GetConstructor(new Type[] { typeof(string), typeof(SelectDataGroup[]), typeof(SelectData[]) });
            }
            return ci;
        }

        protected override object[] GetConstructorParamters(SelectDataGroup item)
        {
            if (item.Items.Count == 0)
            {
                if (item.Groups.Count == 0)
                    return new object[] { item.Name };
                else
                    return new object[] { item.Name, item.Groups.ToArray() };
            }
            else
            {
                if (item.Groups.Count == 0)
                    return new object[] { item.Name, item.Items.ToArray() };
                else
                    return new object[] { item.Name, item.Groups.ToArray(), item.Items.ToArray() };
            }
        }
    }
}
